var autodb = require('../../db/auto.db')
var config = require('../../config')
var fs = require('fs')
var path = require('path')
var async = require('async')
var total = require('../total')
var logger = require('../../logger')
var emit = require('../../emit')

function clear(isMusic, fileid, cb) {
    async.waterfall([
        function (callback) {
            autodb.client(isMusic).del(fileid + '.tags', (err, response) => {
                callback(err);
            })
        },

        function (callback) {
            autodb.client(isMusic).del(fileid, (err, response) => {
                callback(err);
            })
        },

        function (callback) {
            autodb.client(isMusic).srem(config.allfile, fileid, (err, response) => {
                callback(err);
            })
        },

    ], (err) => {
        cb(err);
    })
}

function deletefile(isMusic, fileid, cb) {
    autodb.getfileinfo(isMusic, fileid, (err, info) => {
        if (err) {
            return cb(err);
        }
        if (info) {
            var resourcepath = isMusic ? config.musicResourcePath : config.soundResourcePath;
            var file = path.join(resourcepath, fileid + info.suffix);
            fs.unlink(file, (err) => {
                cb(err);
            })
        } else {
            cb('info is null');
        }
    })
}

function remove(isMusic, fileid, isdeletefile, cb) {
    async.waterfall([
        function (callback) {
            autodb.getfiletags(isMusic, fileid, (err, response) => {
                callback(err, response);
            })
        },

        function (tags, callback) {
            async.forEach(tags, (tag, cb) => {
                autodb.client(isMusic).srem(tag + '.files', fileid, (err) => {
                    if (err) {
                        logger.log("error", "remove ", fileid, " from ", response[i], '.files ', err);
                        return cb(err);
                    }
                    logger.log("remove ", fileid, " from ", tag, '.files succeed');
                    cb();
                })

            }, (err) => {
                callback(err);
            });
        },

        function (callback) {
            if (isdeletefile) {
                deletefile(isMusic, fileid, (err) => {
                    callback(err);
                })
            } else {
                callback(null);
            }
        },

        function (callback) {
            clear(isMusic, fileid, (err) => {
                callback(err);
            });
        },
    ], (err) => {
        cb(err);
    })
}

function onsocket(socket, data) {
    logger.log("removefile", data);
    if (data.ids == null) {
        return;
    }
    async.forEach(data.ids, (id, cb) => {
        remove(data.isMusic, id, true, (err) => {
            cb(err);
        })
    }, (err) => {
        var protocol = data.isMusic ? 'remove-music' : 'remove-sound';
        var result = err ? false : true;
        emit.send(socket, protocol, result);
        if (err) {
            logger.log(err);
        }
        else {
            total.onsocket(socket, {isMusic:data.isMusic});
        }
    })
}

module.exports = {
    remove,
    onsocket
}